(function() {

    var
    $u = juliet.using({
        validationResult : 'juliet.validation.validationResult',
        validator : 'juliet.validation.validator'
    }),
    vr = $u('validationResult');

    /**
     * Regular expression validator
     */
    var RegexClass = function() {

        this.templates = {
            'email' : /^[A-Z0-9\._%+-]+@[A-Z0-9\.-]+\.[A-Z]{2,4}$/i,
            'number' : /^[0-9]*$/,
            'latin' : /^[A-Z]*$/i,
            'latin-and-number' : /^[A-Z0-9]*$/i
        };

        this.validate = function() {
            var
            value = this.chain.fieldValueHelper.fieldValue, o = this.metadata.options,
            regExp = new RegExp(this.templates[o.template] || o.template);

            this.result = (regExp.test(value) ? vr.SUCCESS : vr.FAILED);
        };

        this.getError = function() {
            var
            o = this.metadata.options,
            messageKey = (this.templates[o.template] != undefined) ? 'regex.' + o.template : 'regex';

            return this.base.getError.call(this, messageKey, this.metadata.options);
        };

    };

    $u('validator').addValidator('Regex', RegexClass);

})();
